#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <set>
#define ll long long
using namespace std;
/* #ifdef ONLINE_JUDGE */

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

/* #endif */
ll read() {
    ll p = 0, flg = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') flg = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        p = p * 10 + c - '0';
        c = getchar();
    }
    return p * flg;
}
void write(ll x) {
    if (x < 0) {
        x = -x;
        putchar('-');
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}
bool _stmer;

const int N = 1e5 + 5;
const ll inf = -2e18;

class Matrix {

public:

    ll A[6][6];
    int n, m;

    ll* operator [](int x) { return A[x]; }

    Matrix () : A(), n(), m() {}

    Matrix (int _n, int _m) {
        n = _n, m = _m;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                A[i][j] = inf;
    }

    Matrix (int _n, int _m, int flg) {
        n = _n, m = _m;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                A[i][j] = inf;
        for (int i = 1; i <= n; i++)
            A[i][i] = flg;
    }

    friend Matrix operator *(Matrix x, Matrix y) {
        Matrix ans(x.n, y.m);
        for (int i = 1; i <= x.n; i++)
            for (int k = 1; k <= x.m; k++)
                for (int j = 1; j <= y.m; j++)
                    ans[i][j] = max(ans[i][j], x[i][k] + y[k][j]);
        return ans;
    }
};

array <set <int>, 6> isl;

array <array <ll, 6>, N> h, g;
array <int, N> s;

Matrix getmatrix(int x, bool flg = 0) {
    Matrix ans(5, 5);
    auto it = isl[s[x]].lower_bound(x);
    ans[s[x - 1]][s[x]] = h[x][s[x]];
    if (it != isl[s[x]].begin() && !flg)
        it--, ans[s[x]][s[x]] = g[x][s[x]] - g[*it][s[x]];
    for (int i = 1; i <= 5; i++)
        if (i != s[x]) ans[i][i] = 0;
    return ans;
}

namespace Sgt {

array <Matrix, N * 4> edge;

void build(int x, int l, int r) {
    if (l == r) {
        edge[x] = getmatrix(l);
        return;
    }
    int mid = (l + r) >> 1;
    build(x * 2, l, mid);
    build(x * 2 + 1, mid + 1, r);
    edge[x] = edge[x * 2] * edge[x * 2 + 1];
}

void modify(int x, int l, int r, int k, Matrix y) {
    if (l == r) {
        edge[x] = y;
        return;
    }
    int mid = (l + r) >> 1;
    if (k <= mid) modify(x * 2, l, mid, k, y);
    else modify(x * 2 + 1, mid + 1, r, k, y);
    edge[x] = edge[x * 2] * edge[x * 2 + 1];
}

Matrix query(int x, int l, int r, int L, int R) {
    if (L > r || R < l) return Matrix(5, 5, 0);
    if (L <= l && R >= r) return edge[x];
    int mid = (l + r) >> 1; Matrix ans(5, 5, 0);
    if (L <= mid) ans = ans * query(x * 2, l, mid, L, R);
    if (R > mid) ans = ans * query(x * 2 + 1, mid + 1, r, L, R);
    return ans;
}

} //namespace Sgt


bool _edmer;
int main() {
    cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
#ifndef cxqghzj
    freopen("paint.in", "r", stdin);
    freopen("paint.out", "w", stdout);
#endif
    int n = read(), m = read(), q = read();
    for (int i = 1; i <= n; i++)
        s[i] = read(), isl[s[i]].insert(i);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            h[i][j] = read(), g[i][j] = g[i - 1][j] + h[i][j];
    Sgt::build(1, 1, n);
    Matrix tp = Sgt::query(1, 1, n, 2, n), S(1, 5, 0);
    fill(S[1] + 1, S[1] + 6, 0), S[1][s[1]] = h[1][s[1]], S = S * tp;
    ll lst = S[1][s[n]];
    write(lst), puts("");
    while (q--) {
        int op = read(); ll x = read(), y = read();
        x = x ^ lst, y = y ^ lst;
        if (op == 1) {
            auto it = isl[s[x]].upper_bound(x);
            int _y = s[x];
            isl[s[x]].erase(x);
            s[x] = y;
            isl[s[x]].insert(x);
            Sgt::modify(1, 1, n, x, getmatrix(x));
            if (x < n)
                Sgt::modify(1, 1, n, x + 1, getmatrix(x + 1));
            if (it != isl[_y].end())
                Sgt::modify(1, 1, n, *it, getmatrix(*it));
            it = isl[s[x]].upper_bound(x);
            if (it != isl[s[x]].end())
                Sgt::modify(1, 1, n, *it, getmatrix(*it));
        }
        else {
            for (int i = 1; i <= m; i++) {
                auto it = isl[i].lower_bound(x);
                if (it != isl[i].end())
                    Sgt::modify(1, 1, n, *it, getmatrix(*it, 1));
            }
            tp = Sgt::query(1, 1, n, x + 1, y), S = Matrix(1, 5, 0);
            for (int i = 1; i <= m; i++) {
                auto it = isl[i].lower_bound(x);
                if (it != isl[i].end())
                    Sgt::modify(1, 1, n, *it, getmatrix(*it));
            }
            fill(S[1] + 1, S[1] + 6, 0);
            S[1][s[x]] = h[x][s[x]];
            S = S * tp;
            write(lst = S[1][s[y]]), puts("");
        }
    }
    return 0;
}
